package jannex.sql.extensions;

import jannex.Jannex;
import jannex.internal.ExceptionFactory;
import jannex.sql.JxArray;
import jannex.sql.JxResultSet;
import jannex.sql.TypedArray;

import java.sql.SQLException;
import java.util.Map;

public class ArrayImpl implements JxArray {

    protected final java.sql.Array array;
    protected final Jannex jannex;

    public ArrayImpl(java.sql.Array array, Jannex jannex) {
        this.array = array;
        this.jannex = jannex;
    }

    @Override
    public <E> TypedArray<E> as(Class<E> elementClass) {
        return new TypedArrayImpl<>(array, jannex, elementClass);
    }

    @Override
    public String getBaseTypeName() {
        try {
            return array.getBaseTypeName();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int getBaseType() {
        try {
            return array.getBaseType();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public Object getArray() {
        try {
            return array.getArray();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public Object getArray(Map<String, Class<?>> map) {
        try {
            return array.getArray(map);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public Object getArray(long index, int count) {
        try {
            return array.getArray(index, count);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public Object getArray(long index, int count, Map<String, Class<?>> map) {
        try {
            return array.getArray(index, count, map);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public JxResultSet getResultSet() {
        try {
            return new ResultSetImpl(array.getResultSet(), jannex);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public JxResultSet getResultSet(Map<String, Class<?>> map) {
        try {
            return new ResultSetImpl(array.getResultSet(map), jannex);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public JxResultSet getResultSet(long index, int count) {
        try {
            return new ResultSetImpl(array.getResultSet(index, count), jannex);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public JxResultSet getResultSet(long index, int count, Map<String, Class<?>> map) {
        try {
            return new ResultSetImpl(array.getResultSet(index, count, map), jannex);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public void free() {
        try {
            array.free();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }
}
